import axios from 'axios';
export default {
  data() {
    const item = {

    };
    return {
      selectedBooks: [], // 存储选中的书籍ID
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      isCollapse: false,
      tableData: Array(10).fill(item),
      input: '',
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      circleUrl: "/src/assets/img/logo.png",
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px',
      currentBookId: null, // 新增当前编辑图书ID
    };
  },

  mounted() {
    this.fun();
  },

  methods: {
    // 下拉菜单
    handleClick() {
      alert('button click');
    },
    
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    handleSizeChange(pageSize) {
      console.log(this.pageSize)
      this.pageSize = pageSize
      this.fun()
    },
    handleCurrentChange(pageNum) {
      console.log(this.pageNum)
      this.pageNum = pageNum
      this.fun()
    },
    async deleteBook(id) {
      try {
        await axios.delete(`http://127.0.0.1:8081/books/delete/${id}`);
        alert("删除成功");
        this.fun(); // 重新获取书籍列表以更新页面
      } catch (error) {
        console.error('删除书籍失败:', error);
        alert("删除失败");
      }
    },
    edit(row) {
      this.currentBookId = row.bookId;
      this.form = {
        name: row.bookName,
        author: row.author,
        bookTypeName: row.bookTypeName,
        address: row.address,
        description: row.description,
        status: row.status
      };
      this.dialogFormVisible = true;
    },
    borrow() {
      alert("借阅成功");
    },
    //提交更新图书表单
    async submit() {
      try {
        const response = await axios.post(
          `http://127.0.0.1:8081/books/save`,
          {
            bookId: this.currentBookId,
            bookName: this.form.name,
            author: this.form.author,
            bookTypeName: this.form.bookTypeName,
            address: this.form.address,
            description: this.form.description,
            status: this.form.status
          }
        );
        
        this.$message.success('更新/添加成功');
        // 清空表单
        this.form = {
          name: '',
          author: '',
          address: '',
          bookTypeName: '',
          description: '',
          status: ''
        };
        this.dialogFormVisible = false;
        this.fun(); // 刷新列表
      } catch (error) {
        console.error('更新/添加失败:', error);
        this.$message.error('更新/添加失败');
      }
    },
    // 取消添加/更新
    cancel(){
      this.form = {
        name: '',
        author: '',
        address: '',
        bookTypeName: '',
        description: '',
        status: ''
      };
      this.dialogFormVisible = false;
    },
    // 添加图书
    async addbook() {
      this.currentBookId=null;
      this.dialogFormVisible = true;
    },
    // 分页查询
    fun() {
      axios.get("http://127.0.0.1:8081/books/page", {
        params: {
          page: this.pageNum,
          size: this.pageSize
        }
      })
      .then(response => {
        console.log(response.data);
        this.tableData = response.data.data;
        this.total = response.data.total;
      })
      .catch(err => {
        console.error('分页请求失败:', err);
      });
    },
    
  }
}
